/**
 * 
 */
package server;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;

import javax.imageio.ImageIO;

/**
 * @author Jun
 *
 */
public class DataRead2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		String filePath = "e:\\N20031223G062731.img";
		String filePath2 = "e:\\N20031223G062731.jpg";
//		String filePath2  = "e:\\05.jpg";

//		BufferedImage srcImage = ImageIO.read(new File(filePath2));
//		ByteArrayOutputStream bao=new ByteArrayOutputStream(); 
		
		BufferedImage bi = ImageIO.read(new File(filePath2));
		int[] rbg = new int[bi.getWidth() * bi.getHeight()];
				
//		int[] iArray = GetData(filePath);
		int x = 0;
		int y = 0;
		int w = 512;
		int h = 512;

		bi.getRGB(x, y, w, h, rbg, 0, w);
//		BufferedImage srcImage = new BufferedImage(w, h,
//				BufferedImage.TYPE_USHORT_GRAY);
//
//		srcImage.getRaster().setPixels(x, y, w, h, iArray);
//
//		ImageIO.write(srcImage, "jpg", new File(filePath2));
		

		
//		byte[] data = ((DataBufferByte)srcImage.getData().getDataBuffer()).getData();
		System.out.println("ss");	
	}
	public static int[] GetData(String FileName) throws Exception {
		
		RandomAccessFile rFile = new RandomAccessFile(FileName, "r");

		int[] result = new int[512*512];
		long point = rFile.length() - 524288;
		int i = 0;
		while (rFile.getFilePointer() < rFile.length()) {
			long p = rFile.getFilePointer();
			if (p < point)
				rFile.readByte();
			else {
				result[i++] = getByte(rFile.readByte(),rFile.readByte());
			}
		}
		return result;
	}
	public static int getByte(byte a,byte b){
		return (int) ((b& 0xFF)*256+ (a & 0xFF));	
	}
	public static Image GetImage(String filePath, String filePath2)throws Exception{
		
		RandomAccessFile rFile = new RandomAccessFile(filePath, "r");
		long point = rFile.length() - 512*512*2;
		rFile.seek(point);

		byte[] byteImage = new byte[512*512*2];
		rFile.readFully(byteImage);

		Image img = Toolkit.getDefaultToolkit().createImage(byteImage);
//		BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
//		BufferedImage bufferImage = new BufferedImage();
//		ImageIO.write(bufferImage, "jpg", filePath2);
		
		return img;
	}

}
